﻿using System;
using System.Collections.Generic;
using System.Linq;
using YArchitech.MEP.Electric.Common;

namespace YArchitech.Revit.MEP.Electric.Calculator
{
	internal class FindValue
	{
		public static bool FindClosedValue(List<double> lstValue, double dWillFind, ref double dLeft, ref double dRight, ref InterpolationType InterType)
		{
			lstValue.Sort();
			List<double> list = lstValue.Distinct<double>().ToList<double>();
			if (!list.Contains(dWillFind))
			{
				list.Add(dWillFind);
			}
			if (list.Count < 2)
			{
				return false;
			}
			list.Sort();
			if (MathUtils.IsEqual(dWillFind, list.First<double>(), 0.0001))
			{
				dLeft = (dRight = list.ElementAt(1));
				InterType = InterpolationType.eMinOut;
			}
			else if (MathUtils.IsEqual(dWillFind, list.Last<double>(), 0.0001))
			{
				dLeft = (dRight = list.ElementAt(list.Count - 2));
				InterType = InterpolationType.eMaxOut;
			}
			else
			{
				int num = list.FindIndex((double d) => MathUtils.IsEqual(d, dWillFind, 0.0001));
				if (num <= 0 || num >= list.Count - 1)
				{
					return false;
				}
				dLeft = list.ElementAt(num - 1);
				dRight = list.ElementAt(num + 1);
				InterType = InterpolationType.eIn;
			}
			return true;
		}
	}
}
